package study.cell_game.objects;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.andengine.entity.sprite.Sprite;

import study.cell_game.activity.GameActivity;
import study.cell_game.enums.Direction;
import study.cell_game.objects.textures.TokenTextures;
import study.cell_game.scenes.GameScene;

public class CellMap {
	
	public List<Cell> getInitialCellsForPlayer1() {
		Cell[] cells = new Cell[] { Cells[2], Cells[6], Cells[11], Cells[15], Cells[18] };
		return Arrays.asList(cells);
	}
	
	public List<Cell> getInitialCellsForPlayer2() {
		Cell[] cells = new Cell[] { Cells[0], Cells[3], Cells[7], Cells[12], Cells[16] };
		return Arrays.asList(cells);
	}
	
	private Cell[] Cells = new Cell[19];
	private boolean[] w = new boolean[6];
	private int CellNum = 19;
	int mod(int c){
		if (c<0) return -c;
		return c;
	}
	public CellMap() {
		
		int cenX = 380;
		int offY = 60;
		int index = 0;
		for(int col = -2; col <= 2; col++){
			for(int row = 0; row < 5 - mod(col); row++){
				int x = 65 * col + cenX;
				int y = offY + 75 * row + mod(col) * 35;
				Cells[index] = new Cell(x,y);
				Cells[index].init();
				index++;
			} 
		}
		index = 0;
		//teoretycznie powinno ustawić wszystkie powiązania na planszy.
		for(int col = -2; col <= 2; col++){
			for(int row = 0; row < 5 - mod(col); row++){
				Cell north;
				Cell south;
				Cell southwest;
				Cell northwest;
				if(row == 0){
					north = null;
				} else {
					north = Cells[index - 1];
				}
				if(row == 5 - mod(col) - 1){
					south = null;
				} else {
					south = Cells[index + 1];
				}
				int substractor = (5 - mod(col));
				
				if(col<=0){
					substractor--;
				}
				
				
				if(col == -2 || (col <= 0 && row == 5 - mod(col) - 1)){
					southwest = null;
				} else {
					//requires fixation
					southwest = Cells[index - substractor];
				}
				
				if(col == -2 || (col <= 0 && row == 0)){
					northwest = null;
				} else {
					//requires fixation
					northwest = Cells[index - substractor - 1];
				}
				
				if(northwest != null){
					Cells[index - substractor - 1].getNeighbours()[Direction.SOUTHEAST.getValue()] = Cells[index];
				}
				if(southwest != null){
					Cells[index - substractor].getNeighbours()[Direction.NORTHEAST.getValue()] = Cells[index];
				}
				
				Cell[] cellz = new Cell[6];
				cellz[Direction.NORTH.getValue()] = north;
				cellz[Direction.SOUTH.getValue()] = south;
				cellz[Direction.NORTHWEST.getValue()] = northwest;
				cellz[Direction.SOUTHWEST.getValue()] = southwest;
				
				Cells[index].setNeighbours(cellz);
				
				index++;
			}
		}
		
		
	}

	public int getCellNum() {
		return CellNum;
	}

	public Cell getCell(int n) {
		return Cells[n];
	}
	
	public void setCell(Cell cell, int i)
	{
		Cells[i] = cell;
	}

}
